package cn.zhiran.org.service.impl;

import cn.zhiran.basic.service.impl.BaseServiceImpl;
import cn.zhiran.org.domain.Department;
import cn.zhiran.org.mapper.DepartmentMapper;
import cn.zhiran.org.query.DepartmentQuery;
import cn.zhiran.org.service.IDepartmentService;
import cn.zhiran.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service

public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;


    @Transactional
    @Override
    public void update(Department department) {
        //拼一个dirPath
        String dirPath="";
        //如果添加的是一个顶级部门
        if(department.getParent()==null || department.getParent().getId()==null){
            dirPath="/"+department.getId();
        }else{
            Department department1 = departmentMapper.loadById(department.getParent().getId());
            dirPath=department1.getDirPath()+"/"+department.getId();
        }
        //把dirPath放入
        department.setDirPath(dirPath);

        departmentMapper.update(department);
    }

    @Transactional
    @Override
    public void insert(Department department) {
        departmentMapper.insert(department);
        //拼一个dirPath
        String dirPath="";
        //如果添加的是一个顶级部门
        if(department.getParent()==null){
           dirPath="/"+department.getId();

        }else{
            Department department1 = departmentMapper.loadById(department.getParent().getId());
            System.out.println(department1);
            dirPath=department1.getDirPath()+"/"+department.getId();
            System.out.println(dirPath);
        }
            //把dirPath放入
                department.setDirPath(dirPath);
            //更新数据库
            update(department);

    }



    @Override
    public List<Department> tree() {
        return departmentMapper.tree();
    }
}
